package com.app.controller;
import com.alibaba.fastjson.JSONObject;
import com.app.enums.StatusCodeEnum;
import com.app.mapper.jiuquan.ComplaintMapper;
import com.app.model.ComplainModel;
import com.app.model.JiuquanUserModel;
import com.app.service.ComplaintService;
import com.app.service.UserService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.ComplainReturnVo;
import com.app.vo.ComplaintVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;




@RestController
@Controller
@RequestMapping("/jiuquan/complaint")
public class ComplainController {

    @Autowired
    ComplaintMapper complaintMapper;

    @Autowired
    ComplaintService complaintService;

    @Autowired
    private UserService userService;

    /**
     * 各地区数据通讯接口
     * @param dataJson
     */
    @PostMapping("/doProtectionState")
    public void addAndUpdateMentorInfo(@RequestBody JSONObject dataJson) {
        if(dataJson!=null)
        {
            ComplainModel complainModel = new ComplainModel();
            complainModel.setDataJson(dataJson.toString());
            String code = dataJson.getString("code");
            Integer areaId = dataJson.getInteger("areaId");
            Integer userId = dataJson.getInteger("userId");
            complainModel.setAreaCode(code);
            complainModel.setAreaId(areaId);
            complainModel.setUserId(userId);
            System.out.println("地区编号："+areaId+"=====================");
            System.out.println("code："+code+"************************************");
            ComplainModel model = complaintService.getModelByAreaIdAndCode(areaId,code);
            if(model!=null)
            {
                complaintMapper.deleteById(model.getId());
            }
            complaintMapper.insert(complainModel);
        }


    }

    /**
     * 根据code查询详情
     * @param code
     * @return
     */
    @GetMapping("/selectSchedule")
    public Result getCommentList(String code) {
        ComplainModel model = complaintService.getModelByCode(code);
        return Result.success(model.getDataJson());
    }

    /**
     * 管理后台列表
     * @param model
     * @return
     */
    @GetMapping("/getComplaintList")
    public Result getCommentList(ComplainModel model) {
        ComplainReturnVo vo = complaintService.getList(model);
        return Result.success(vo);
    }

    /**
     * 管理后台列表
     * @param id
     * @return
     */
    @GetMapping("/getComplaint")
    public Result getComplaint(Integer id) {
        ComplaintVo vo = complaintService.getModelById(id);
        return Result.success(vo);
    }

    /**
     * 查询我的投诉
     * @param request
     * @return
     */
    @GetMapping("/getMyComplaint")
    public Result getMyComplaint(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.verify(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
        ComplainModel model = new ComplainModel();
        model.setId(user.getId());
        model.setPage(1);
        model.setLimit(9999);
        ComplainReturnVo vo = complaintService.getList(model);
        return Result.success(vo.getList());
    }


}